package com.springbootes;

import ch.qos.logback.core.util.TimeUtil;
import com.alibaba.fastjson.JSON;
import com.springbootes.pojo.User;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @User 小菜鸡
 * @Data 2020/5/16
 */
//@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ac = SpringApplication.run(SpringbootApplication.class, args);
        RestHighLevelClient restHighLevelClient = (RestHighLevelClient)ac.getBean("restHighLevelClient");
        //System.out.println(restHighLevelClient);

        //构建create类型的文档对象，初始化文档索引
        //CreateIndexRequest createRequest = new CreateIndexRequest("lin_index");
        //创建索引，返回创建的结果
        //CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createRequest, RequestOptions.DEFAULT);
        //System.out.println(createIndexResponse);

        //构建get类型的文档对象，初始化文档索引
//        GetIndexRequest getIndexRequest = new GetIndexRequest("lin_index");
        //判断索引是否存在
//        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
//        System.out.println(exists);

        //构建delete类型的文档对象，初始化文档索引
//        DeleteIndexRequest deleteRequest = new DeleteIndexRequest("lin_index");
//        AcknowledgedResponse deleteResponse = restHighLevelClient.indices().delete(deleteRequest,RequestOptions.DEFAULT);
//        System.out.println(deleteResponse);

        //添加文档
//        User lin = new User("lin", 22);
//        //创建文档的请求对象，也是文档的索引，初始化文档索引
//        IndexRequest request = new IndexRequest("test");
//        //文档id，文档的默认类型是_doc
//        request.id("1");
          //请求的超时时间，如果请求超过了这个设置的时间，那么请求就不再被执行，这个参数不用设置也是可以的
//        request.timeout(TimeValue.timeValueSeconds(1));
//        //文档的数据内容，注意要将数据转化为json对象
//        request.source(JSON.toJSONString(lin), XContentType.JSON);
//        //创建文档，返回文档的创建结果,这个方法可以做文档的新增也可以做文档的修改
//        IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
//        System.out.println(indexResponse);

        //判断文档是否存在，并获取文档信息
        //创建文档的请求对象，初始化索引、文档id
        //不知道为啥这条数据显示不出来
        //GetRequest getRequest = new GetRequest("test", "1");
        //这条数据可以显示出来
        //GetRequest getRequest1 = new GetRequest("test","doc1");
        //应该是对查询结果的过滤，就是需要显示哪些字段、过滤掉哪些字段
        //getRequest.fetchSourceContext(new FetchSourceContext(false));
        //根据某个字段来进行排序？
        //getRequest.storedFields("_none_");
        //判断请求的索引中的文档id是否存在
//        boolean exists1 = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
//        System.out.println(exists1);
//        if (exists1){
//            //获取对应的文档对象
//            GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
//            System.out.println(documentFields);
//            //将获取到的文档对象转换成字符串输出
//            System.out.println(documentFields.getSourceAsString());
//        }

        //更新文档信息
        //创建更新类型的文档对象，初始化文档索引、文档id
        //UpdateRequest updateRequest = new UpdateRequest("test", "1");
        //创建要更新的数据对象
        //User user_update = new User("lin_update", 22);
        //将数据对象转化为json，并高数es传过去的是一个json对象，当然也可以传过去其他类型的对象例如xml类型...
        //updateRequest.doc(JSON.toJSONString(user_update),XContentType.JSON);
        //进行更新，返回更新的结果
        //UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        //System.out.println(updateResponse);

        //删除文档信息
        //创建删除类型的文档对象
        //DeleteRequest deleteRequest = new DeleteRequest("test", "1");
        //利用restHighLevelClient执行删除文档的操作
        //DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        //System.out.println(deleteResponse);

        //批量操作，这里以批量插入为例
//        BulkRequest bulkRequest = new BulkRequest();
//        ArrayList<User> users = new ArrayList<>();
        //集合中批量添加对象
//        for (int i=0;i<5;i++){
//            users.add(new User("lin"+i,i));
//        }
        //将请求添加进bulkRequest对象
//        for (int i = 0; i < users.size(); i++) {
//            //将所有的请求对象添加进批处理的bulkRequest对象中
//            bulkRequest.add(new IndexRequest("test").id("" + i).
//                    source(JSON.toJSONString(users.get(i)), XContentType.JSON));
//        }
        //执行批处理请求，返回执行的结果
//        BulkResponse bulkItemResponses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//        System.out.println(bulkItemResponses);

        //查询的复杂操作
        //构建文档search的请求对象，初始化索引
        SearchRequest searchRequest = new SearchRequest("test");
        //构建SearchSourceBuilder对象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //查询的条件可以使用QueryBuilders工具来构建
        //QueryBuilders.termQuery  #精确匹配
        //QueryBuilders.matchAllQuery()  #匹配所有
        //查询的属性名、属性值
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "lin0");
        //将TermQueryBuilder对象添加到SearchSourceBuilder对象中
        sourceBuilder.query(termQueryBuilder);
        //设置请求的时间
        //sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //设置分页，页面的初始值
//        sourceBuilder.from(1);
        //页面的数据量
//        sourceBuilder.size(10);
        //将SearchSourceBuilder对象添加进SearchRequest对象中
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        //以集合的形式显示数据
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }
    }
}
